<template>
  <view class="publish-container">
    <!-- 表单主体 -->
    <scroll-view class="form-content" scroll-y>
      <!-- 标题输入 -->
      <view class="form-card">
        <view class="floating-label">
          <text class="label-text" :class="{ focused: titleFocused || model.title }">活动标题</text>
          <input 
            class="title-input"
            v-model="model.title"
            placeholder=" "
            @focus="titleFocused = true"
            @blur="titleFocused = false"
          />
        </view>
      </view>

      <!-- 内容输入 -->
      <view class="form-card">
        <view class="floating-label">
          <text class="label-text" :class="{ focused: contentFocused || model.content }">活动详情</text>
          <textarea 
            class="content-input"
            v-model="model.content"
            placeholder=" "
            auto-height
            @focus="contentFocused = true"
            @blur="contentFocused = false"
          />
          <text class="word-count">{{ model.content.length }}/500</text>
        </view>
      </view>

      <!-- 媒体上传 -->
      <view class="form-card">
        <view class="upload-section">
          <text class="section-title">添加图片/视频</text>
          <view class="media-grid">
            <view 
              v-for="(item, index) in model.media"
              :key="index"
              class="media-item"
            >
              <image 
                :src="item.url"
                mode="aspectFill"
                class="media-preview"
              />
              <view class="delete-mask" @click="removeMedia(index)">
                <uni-icons type="close" size="24" color="#fff" />
              </view>
            </view>
            <view 
              class="upload-card"
              @click="handleUpload"
            >
              <uni-icons type="plus" size="48" color="#007AFF" />
              <text class="upload-tip">点击上传</text>
            </view>
          </view>
        </view>
      </view>
    </scroll-view>

    <!-- 发布按钮 -->
    <view class="publish-footer">
      <button 
        class="publish-btn"
        :class="{ disabled: !formValid }"
        @click="handleSubmit"
      >
        <uni-icons type="checkmark" size="20" color="#fff" />
        立即发布
      </button>
    </view>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue'
import request from '../../utils/request'
const model = ref({
  title: '',
  content: '',
  media: []
})

const titleFocused = ref(false)
const contentFocused = ref(false)

// 表单验证
const formValid = computed(() => {
  return model.value.title.trim() && model.value.content.trim()
})

// 媒体上传处理
const handleUpload = async () => {
  try {
    const res = await uni.chooseMedia({
      count: 9,
      mediaType: ['image', 'video']
    })
    const files = res.tempFiles.map(file => ({
      url: file.tempFilePath,
      type: file.fileType
    }))
    model.value.media.push(...files)
  } catch (error) {
    console.log('上传取消', error)
  }
}

// 删除媒体
const removeMedia = (index) => {
  model.value.media.splice(index, 1)
}
const handleSubmit = async () => {
  if (!formValid.value) return;

  // 获取本地存储的 storeId
  const storeId = uni.getStorageSync('storeId');
  if (!storeId) {
    uni.showToast({ title: '未获取到店铺信息', icon: 'none' });
    return;
  }

  // 准备请求参数
  const params = {
    title: model.value.title,
    content: model.value.content,
    storeId: storeId,
    activityStatus: 2, // 直接设置为下架
    image: null, // 如果有图片需要上传，可以在这里处理
    auditStatus: 3, // 直接设置为未审核
  };

  // 打印提交的数据
  console.log('提交的数据:', JSON.stringify(params, null, 2));

  // 显示加载中
  uni.showLoading({ title: '发布中...' });

  // 调用封装好的 request 函数
  request({
    url: '/mstore/activity/add', // 接口地址
    method: 'POST',
    data: params,
  })
    .then((res) => {
      if (res.code === 200) {
        uni.showToast({ title: '发布成功' });
        uni.navigateBack(); // 返回上一页
      } else {
        uni.showToast({ title: '发布失败，请重试', icon: 'none' });
      }
    })
    .catch((err) => {
      console.error('发布失败:', err);
      uni.showToast({ title: '发布失败，请重试', icon: 'none' });
    })
    .finally(() => {
      uni.hideLoading(); // 隐藏加载中
    });
};

</script>

<style lang="scss" scoped>
.publish-container {
  height: 100vh;
  background: #f8f9fa;
  display: flex;
  flex-direction: column;
}

.form-content {
  flex: 1;
  padding: 24rpx;
}

.form-card {
  background: #fff;
  border-radius: 16rpx;
  padding: 32rpx;
  margin-bottom: 24rpx;
  box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.05);
}

.floating-label {
  position: relative;
  padding-top: 24rpx;

  .label-text {
    position: absolute;
    left: 0;
    top: 40rpx;
    font-size: 32rpx;
    color: #666;
    transition: all 0.2s ease;
    transform-origin: left center;

    &.focused {
      transform: translateY(-28rpx) scale(0.8);
      color: #007AFF;
    }
  }
}

.title-input {
  height: 96rpx;
  font-size: 32rpx;
  padding-top: 16rpx;
  border-bottom: 2rpx solid #e0e0e0;

  &:focus {
    border-color: #007AFF;
  }
}

.content-input {
  width: 100%;
  min-height: 200rpx;
  font-size: 28rpx;
  padding-top: 16rpx;
  border-bottom: 2rpx solid #e0e0e0;
}

.word-count {
  position: absolute;
  right: 0;
  bottom: 8rpx;
  font-size: 24rpx;
  color: #999;
}

.upload-section {
  .section-title {
    display: block;
    font-size: 28rpx;
    color: #666;
    margin-bottom: 32rpx;
  }
}

.media-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 16rpx;

  .media-item {
    width: 200rpx;
    height: 200rpx;
    border-radius: 8rpx;
    overflow: hidden;
    position: relative;

    .media-preview {
      width: 100%;
      height: 100%;
    }

    .delete-mask {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: rgba(0,0,0,0.5);
      display: flex;
      align-items: center;
      justify-content: center;
      opacity: 0;
      transition: opacity 0.2s;

      &:active {
        opacity: 1;
      }
    }

    &:active .delete-mask {
      opacity: 1;
    }
  }

  .upload-card {
    width: 200rpx;
    height: 200rpx;
    border: 2rpx dashed #007AFF;
    border-radius: 8rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: rgba(0,122,255,0.05);

    .upload-tip {
      font-size: 24rpx;
      color: #007AFF;
      margin-top: 16rpx;
    }
  }
}

.publish-footer {
  padding: 24rpx;
  background: linear-gradient(transparent, #f8f9fa 50%);

  .publish-btn {
    background: #007AFF;
    color: #fff;
    height: 96rpx;
    border-radius: 48rpx;
    font-size: 32rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;

    &.disabled {
      background: #e0e0e0;
      filter: grayscale(1);
    }

    &:active:not(.disabled) {
      transform: scale(0.98);
      opacity: 0.9;
    }
  }
}
</style>